typedef ConvertSet<T> = T Function(String value);

class MapUtil {

  static int getInt(Map<String, Object?> map, String name) {
    return map['$name'] as int;
  }

  static int? getIntOrNull(Map<String, Object?> map, String name) {
    if (map.containsKey(name)) {
      var data = map[name];
      if (data != null) {
        return data as int;
      }
    }
    return null;
  }

  static String getString(Map<String, Object?> map, String name) {
    return map['$name'] as String;
  }

  static String? getStringOrNull(Map<String, Object?> map, String name) {
    if (map.containsKey(name)) {
      var data = map[name];
      if (data != null) {
        return data as String;
      }
    }
    return null;
  }

  static bool getBoolean(Map<String, Object?> map, String name) {
    var num = map['$name'] as int;
    return num == 1;
  }

  static DateTime getDate(Map<String, Object?> map, String name) {
    var str = map['$name'] as String;
    return DateTime.parse(str);
  }

  static DateTime? getDateOrNull(Map<String, Object?> map, String name) {
    if (map.containsKey(name)) {
      var data = map[name];
      if (data != null) {
        return DateTime.parse(data.toString());
      }
    }
    return null;
  }

  static Set<T> getSet<T>(Map<String, Object?> map, String name, ConvertSet<T> function) {
    String data = getString(map, name);
    Set<T> result = data.split(",").map((s){return function(s);}).toSet();
    return result;
  }

  static Set<T>? getSetOrNull<T>(Map<String, Object?> map, String name, ConvertSet<T> function) {
    String? data = getStringOrNull(map, name);
    Set<T>? result = data?.split(",").map((s){return function(s);}).toSet();
    return result;
  }
}